package com.jie.server.mvc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.common.http.constant.HttpEntityClassNameConstant;
import com.jie.common.http.constant.HttpHeaderLineNameConstant;
import com.jie.common.http.constant.response.ServerResponseStatusConstant;
import com.jie.common.http.entity.request.HttpRequest;
import com.jie.common.http.entity.response.HttpResponse;
import com.jie.common.util.JsonUtil;
import com.jie.server.core.annotation.Mapping;
import com.jie.server.mvc.model.entity.User;
import com.jie.server.mvc.model.vo.UserQueryVo;
import com.jie.server.mvc.model.vo.UserUpdateVo;
import com.jie.server.mvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author XieZhiJie
 * @since 2022-04-24
 */
@Controller("user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 注册
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse register(HttpRequest httpRequest, HttpResponse httpResponse) {
        User user = JsonUtil.fromJson(httpRequest.getEntityString(), User.class);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.userService.register(user)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.REGISTER_SUCCESS);
            httpResponse.setEntity("注册成功!");
        } else {
            httpResponse.setEntity("注册失败! 注册信息为空或帐号已重复!");
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.REGISTER_FAIL);
        }
        return httpResponse;
    }

    /**
     * 登录
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse login(HttpRequest httpRequest, HttpResponse httpResponse) {
        User user = JsonUtil.fromJson(httpRequest.getEntityString(), User.class);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        Integer cookie = this.userService.login(user);
        if (cookie == null) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.LOGIN_FAIL);
            httpResponse.setEntity("登录失败, 帐号或密码错误!");
        } else if (cookie == -1) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.LOGIN_FAIL);
            httpResponse.setEntity("该帐号已登录!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.COOKIE, String.valueOf(cookie));
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_STRING_CLASS_NAME);
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.LOGIN_SUCCESS);
            // 获取用户信息
            httpResponse.setEntity(JsonUtil.toJson(this.userService.getById(cookie)));
        }
        return httpResponse;
    }

    /**
     * 获取当前用户的信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getInfo(HttpRequest httpRequest, HttpResponse httpResponse) {
        String userId = httpRequest.getEntityString();
        if ("".equals(httpRequest.getEntityString())) {
            userId = httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE);
        }
        User user = this.userService.getById(userId);
        if (user != null && !userId.equals(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE))) {
            user.setUserPassword(null);
        }
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_STRING_CLASS_NAME);
        httpResponse.setEntity(JsonUtil.toJson(user));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.GET_USER_INFO_SUCCESS);
        return httpResponse;
    }

    /**
     * 分页获取用户列表
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getUserList(HttpRequest httpRequest, HttpResponse httpResponse) {
        UserQueryVo userQueryVo = JsonUtil.fromJson(httpRequest.getEntityString(), UserQueryVo.class);
        Page<User> page = new Page<>(userQueryVo.getCurrent(), userQueryVo.getLimit());
        this.userService.page(page);
        List<User> list = page.getRecords();
        httpResponse.addOneHeaderLine("total", String.valueOf(page.getTotal()));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.setEntity(JsonUtil.toJson(list));
        return httpResponse;
    }

    /**
     * 返回在线用户列表
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getOnlineUserList(HttpRequest httpRequest, HttpResponse httpResponse) {
        List<User> list = this.userService.getOnlineUserList();
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.GET_ONLINE_USER_LIST_SUCCESS);
        httpResponse.setEntity(JsonUtil.toJson(list));
        return httpResponse;
    }

    /**
     * 更新用户信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse update(HttpRequest httpRequest, HttpResponse httpResponse) {
        Integer userId = Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE));
        UserUpdateVo userUpdateVo = JsonUtil.fromJson(httpRequest.getEntityString(), UserUpdateVo.class);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.userService.update(userId, userUpdateVo)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.UPDATE_USER_INFO_SUCCESS);
            httpResponse.setEntity("修改成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.UPDATE_USER_INFO_FAIL);
            httpResponse.setEntity("修改失败!");
        }
        return httpResponse;
    }

}
